//
//  CZDetailCell.swift
//  GZWeibo666
//
//  Created by peng on 16/05/1.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZDetailCell: UITableViewCell
{
    var commants: CZDetailCommant? {
        // 当别人设置commants属性时,来设置顶部视图内容
        didSet {
            // 设置头像头像
            if let profile_image_url = self.commants?.user?.profile_image_url {
                // 使用SDWebImage下载头像
                iconView.sd_setImageWithURL(NSURL(string: profile_image_url), placeholderImage: UIImage(named: "avatar"))
            }
            
            // 设置用户名称
            // = 右边可以使用?是因为nameLabel.text是一个可选
            nameLabel.text = commants?.user?.screen_name
            
            //MARK: - 评论的时间和来源
            
            // 时间
//            timeLabel.text = commants?.created_at
            
           timeLabel.text = NSDate.sinaStringToDate(commants?.created_at ?? "刚刚").sinaDateDescription()
            
            // 来源
            sourceLabel.text = commants?.source
            
            // 设置认证图标,根据不同的数字来设置不同的图片
            verifiedImageView.image = commants?.user?.verifiedImage
            
            // 会员等级
            
            mbrankImageView.image = commants?.user?.mbrankImage
            
            //内容
            contentLabel.text = commants?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        
        //添加点击
        
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加子控件
        contentView.addSubview(separatorView)
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(mbrankImageView)
        contentView.addSubview(verifiedImageView)
        contentView.addSubview(contentLabel)
        
        // 添加约束
        /// 分割视图.在topView(self)内部左上
        separatorView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 1))
        
        /// 用户头像,在topView底部,左下有间距
        iconView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: separatorView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: CZStatusCellMargin, y: CZStatusCellMargin))
        
        /// 用户名称
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 会员图标
        mbrankImageView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 时间
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 来源
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: timeLabel, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 认证图标
        verifiedImageView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
        
        /// 微博内容在 顶部控件 左下
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: CZStatusCellMargin+4))

        /// 宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:(UIScreen.mainScreen().bounds.width - self.iconView.frame.width-CZStatusCellMargin*8)))
        
        //   contentLabel.frame = CGRect(x: 0, y: 0, width: 375, height: 300)
    }
    
    // MARK: - 计算行高
    /// 计算行高
    func rowHeight(commants: CZDetailCommant) -> CGFloat {
        // 根据模型来设置内容, 当我们把传入进来的模型赋值给self.status cell里面的内容被设置成为status的内容
        self.commants = commants
        
        // 根据新的内容更新约束
        self.layoutIfNeeded()
        
        // 获取底部视图的最大Y值(cell的高度)
        return CGRectGetMaxY(contentLabel.frame) + 10
    }
    
    
    
    // MARK: - 懒加载
    /// 用户头像
    private lazy var iconView: UIImageView =
    {
        let icon =  UIImageView()
        
        icon.layer.cornerRadius = 17.5
        icon.layer.masksToBounds = true
        
        return icon
    }()
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
    
    /// 时间
    private lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    
    /// 来源
    private lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 9)
    
    /// 会员等级图标
    private lazy var mbrankImageView: UIImageView = UIImageView()
    
    /// 认证类型图标
    private lazy var verifiedImageView:  UIImageView = UIImageView()
    
    /// 顶部分割视图
    private lazy var separatorView: UIView = {
        let view = UIView()
        
        // 背景
        view.backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        return view
    }()
    
    /// 评论文本内容
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        
        // 设置换行
        label.numberOfLines = 0
        
        return label
    }()
}

